package com.example.demo.controller;


import com.example.demo.identity.Music;
import com.example.demo.mapper.MusicMapper;
import com.example.demo.services.MusicService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/music")
public class MusicController {
    private final MusicMapper musicMapper;
    private final MusicService musicService;

    public MusicController(MusicMapper musicMapper, MusicService musicService) {
        this.musicMapper = musicMapper;
        this.musicService = musicService;
    }

//    上传音乐文件


    //    获取音乐文件
    @GetMapping("/get-music-metadata")
    public String getMusicMetadata() {
        return "music-metadata";
    }


    //    随机获取音乐
    @GetMapping("/random-music")
    public Map<String, Object> getRandomMusic() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", musicMapper.randomMusic());
        return map;
    }


    //    通过musicKey在minio中获取音频文件的访问链接
    @PostMapping("/get-music-url")
    public Map<String, Object> getMusicUrl(@RequestParam("musicKey") String musicKey) {
        System.out.println(musicKey);
        String musicUrl = musicService.getMusicUrl(musicKey);
        Map<String, Object> map = new HashMap<>();
        Map<String, String> data = new HashMap<>();
        if (musicUrl != null && !musicUrl.isEmpty()) {
            data.put("status", "success");
            data.put("musicUrl", musicUrl);
        } else {
            data.put("status", "fail");
            data.put("musicUrl", "");
        }
        map.put("data", data);
        return map;
    }


    //    通过musicKey获取minio中的歌词文件
    @PostMapping("/get-music-lyrics")
    public Map<String, Object> getMusicLyrics(@RequestParam("musicKey") String musicKey) {
        String lyricContent = musicService.getMusicLrcFile(musicKey);
        Map<String, Object> map = new HashMap<>();
        Map<String, String> data = new HashMap<>();
        if (lyricContent != null && !lyricContent.isEmpty()) {
            data.put("status", "success");
            data.put("lyricContent", lyricContent);
        } else {
            data.put("status", "fail");
            data.put("lyricContent", "");
        }
        map.put("data", data);
        return map;
    }


    //    获取用户喜欢的列表
    @GetMapping("/get-user-music-like-list")
    public Map<String, Object> getUserMusicLikeList(@RequestParam("userId") String userId) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        List<Music> musicList = musicMapper.getUserLikeList(userId);
        if (musicList != null && !musicList.isEmpty()) {
            data.put("status", "success");
            data.put("musicList", musicList);
        } else {
            data.put("status", "fail");
            data.put("musicList", "");
        }
        map.put("data", data);
        return map;
    }

    //    添加喜欢的音乐
    @PostMapping("/add-like-music")
    public Map<String, Object> addLikeMusic(@RequestParam("uuId") String uuId, @RequestBody Map<String, String> requestData) {
        Map<String, Object> map = new HashMap<>();
        Map<String, String> data = new HashMap<>();

//        提取请求体中的数据
        String musicKey = requestData.get("music_key");
        String musicName = requestData.get("music_name");
        String singer = requestData.get("singer");

//        检查是否已经存在相同的记录
        boolean exists = musicMapper.checkExists(uuId, musicKey) > 0;

        if (exists) {
            // 如果记录已存在，返回提示信息
            data.put("status", "exists");
            data.put("message", "您已经添加过这首音乐了！");
        } else {
            // 如果记录不存在，尝试插入
            int addStatus = musicMapper.addFavorites(uuId, musicKey, musicName, singer);
            if (addStatus == 1) {
                data.put("status", "success");
                data.put("message", "添加成功");
            } else {
                data.put("status", "fail");
                data.put("message", "出现未知的错误");
            }
        }
        map.put("data", data);
        return map;
    }

}
